package com.ysd.lis.service.inti.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.inti.IntInstrResultNoChannel;
import com.ysd.lis.entity.lab.LabChannel;
import com.ysd.lis.entity.lab.LabInstrSetup;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.lab.LabRptGroup;
import com.ysd.lis.mapper.inti.IntInstrResultNoChannelMapper;
import com.ysd.lis.mapper.lab.LabItemDictMapper;
import com.ysd.lis.request.inti.IntinstrReusltDto;
import com.ysd.lis.request.lab.IntInstrResultParam;
import com.ysd.lis.service.inti.IntInstrResultNoChannelService;
import com.ysd.lis.service.lab.LabChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-6-3
 */
@Service
public class IntInstrResultNoChannelServiceImpl extends ServiceImpl<IntInstrResultNoChannelMapper, IntInstrResultNoChannel> implements IntInstrResultNoChannelService {

    @Autowired
    IntInstrResultNoChannelMapper intInstrResultNoChannelMapper;
    @Autowired
    LabItemDictMapper labItemDictMapper;
    @Autowired
    LabChannelService labChannelService;

    @Override
    public Result findInstrResultNoChannelList(IntInstrResultParam param) {
        //根据报告单元查出所有的报告项目
        MPJLambdaWrapper<LabItemDict> queryWrapper1 = new MPJLambdaWrapper<>();
        queryWrapper1.selectAll(LabItemDict.class)
                .eq(LabItemDict::getRptGroup, param.getRptGroup())
                .eq(LabItemDict::getDelFlag, 0)
                .orderByAsc(LabItemDict::getSeq);
        List<LabItemDict> labItemDicts = labItemDictMapper.selectList(queryWrapper1);
        List<String> itemDictIds = labItemDicts.stream().map(LabItemDict::getItemNo).collect(Collectors.toList());

        MPJLambdaWrapper<IntInstrResultNoChannel> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(IntInstrResultNoChannel.class)
                .selectAs("rpt", LabRptGroup::getId, IntInstrResultNoChannel::getRptGroupId)
                .selectAs("rpt", LabRptGroup::getRptGroup, IntInstrResultNoChannel::getRptGroupCode)
                .selectAs("ins", LabInstrSetup::getId, IntInstrResultNoChannel::getInstrSetupId)
                .selectAs("ins", LabInstrSetup::getInstrName, IntInstrResultNoChannel::getInstrName)
                .selectAs("cha", LabChannel::getId, IntInstrResultNoChannel::getChannelId)
                .selectAs("dic", LabItemDict::getId, IntInstrResultNoChannel::getItemDictId)
                .innerJoin(LabRptGroup.class, "rpt", p -> p.eq(LabRptGroup::getRptGroup, param.getRptGroup()).eq(LabRptGroup::getDelFlag, 0).and(wrapper -> wrapper.eq(LabRptGroup::getRptGroup, IntInstrResultNoChannel::getRptGroup).or().eq(LabRptGroup::getRptGroup, IntInstrResultNoChannel::getRptGroup)))
                .leftJoin(LabInstrSetup.class, "ins", p -> p.eq(LabInstrSetup::getDelFlag, 0).eq(LabInstrSetup::getRptGroup, LabRptGroup::getRptGroup).and(wrapper -> wrapper.eq(LabInstrSetup::getId, IntInstrResultNoChannel::getInstrCode).or().eq(LabInstrSetup::getInstrCode, IntInstrResultNoChannel::getInstrCode)))
                .leftJoin(LabItemDict.class, "dic", p -> p.eq(LabItemDict::getDelFlag, 0).eq(LabItemDict::getItemNo, LabChannel::getItemNo).eq(LabItemDict::getRptGroup, LabRptGroup::getRptGroup))
                .leftJoin(LabChannel.class, "cha", p -> p.eq(LabChannel::getDelFlag, 0).eq(LabChannel::getChannelCode, IntInstrResultNoChannel::getItemNo).eq(LabChannel::getInstrCode, LabInstrSetup::getInstrCode).eq(LabChannel::getRptGroup, LabRptGroup::getRptGroup).in(LabChannel::getItemNo, itemDictIds))
                .eq(IntInstrResultNoChannel::getDelFlag, 0)
                .eq(IntInstrResultNoChannel::getSampleNo, param.getSampleNo())
                .eq(IntInstrResultNoChannel::getIsHandle, false);

        if (StringUtils.isNotBlank(param.getSampleDaStr())) {
            Timestamp startTime = Timestamp.valueOf(param.getSampleDaStr() + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(param.getSampleDaStr() + " 23:59:59");
            queryWrapper.ge(IntInstrResultNoChannel::getSampleda, startTime);
            queryWrapper.le(IntInstrResultNoChannel::getSampleda, endTime);
        }
        List<IntInstrResultNoChannel> noChannelList = intInstrResultNoChannelMapper.selectList(queryWrapper);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("noChannelList", noChannelList);
        returnMap.put("labItemDictList", labItemDicts);

        return Result.succ(1, "查询成功", returnMap);

    }

    @Override
    public Result saveBacthNoChannelResult(IntinstrReusltDto dto) {
        List<IntInstrResultNoChannel> intInstrResultNoChannelList = dto.getIntInstrResultNoChannelList();
        for (IntInstrResultNoChannel intInstrResultNoChannel : intInstrResultNoChannelList) {
            //更新通道号数据
            LabChannel labChannel = new LabChannel();
            //labChannel.setId(intInstrResultNoChannel.getChannelId());
            labChannel.setChannelCode(intInstrResultNoChannel.getItemNo());
            labChannel.setItemNo(intInstrResultNoChannel.getItemNo());
            labChannel.setInstrCode(intInstrResultNoChannel.getInstrCode());
            labChannel.setRptGroup(intInstrResultNoChannel.getRptGroup());
            labChannel.setSeq("1");
            LambdaQueryWrapper<LabChannel> lambdaQueryWrappers = new LambdaQueryWrapper<>();
            lambdaQueryWrappers.eq(LabChannel::getChannelCode, intInstrResultNoChannel.getItemNo())
                    .eq(LabChannel::getItemNo, intInstrResultNoChannel.getItemNo())
                    .eq(LabChannel::getInstrCode, intInstrResultNoChannel.getInstrCode())
                    .eq(LabChannel::getRptGroup, intInstrResultNoChannel.getRptGroup())
                    .eq(LabChannel::getDelFlag, 0);
            boolean b = labChannelService.saveOrUpdate(labChannel, lambdaQueryWrappers);
            if (b) {
                //将所有得此数据全部更新为已处理

                LambdaUpdateWrapper<IntInstrResultNoChannel> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.set(IntInstrResultNoChannel::getIsHandle, true)
                        .eq(IntInstrResultNoChannel::getRptGroup, intInstrResultNoChannel.getRptGroup())
                        .eq(IntInstrResultNoChannel::getItemNo, intInstrResultNoChannel.getItemNo())
                        .eq(IntInstrResultNoChannel::getInstrCode, intInstrResultNoChannel.getInstrCode());
                int update = intInstrResultNoChannelMapper.update(null, lambdaUpdateWrapper);

            }

        }

        return Result.succ(1, "处理成功", null);
    }
}
